public class MaximumNumberOfBalloons {
    /**
     * 给你一个字符串text，你需要使用 text 中的字母来拼凑尽可能多的单词"balloon"（气球）。
     * 字符串text 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词"balloon"。
     * 链接：https://leetcode-cn.com/problems/maximum-number-of-balloons
     * @param text
     * @return
     */
    public  static int maxNumberOfBalloons(String text) {
        if (text.length()<8)
            return 0;
        int b_num = 0;
        int a_num = 0;
        int l_num = 0;
        int o_num = 0;
        int n_num = 0;
        for (int i=0;i<text.length();i++){
            if (text.charAt(i)=='b'){
                b_num++;
            }else if(text.charAt(i)=='a'){
                a_num++;
            }else if(text.charAt(i)=='l'){
                l_num++;
            }else if (text.charAt(i)=='o'){
                o_num++;
            }else if (text.charAt(i)=='n'){
                n_num++;
            }
        }
        if (b_num<1||a_num<1||l_num<2||o_num<2||n_num<1){
            return 0;
        }
        int ans =0;
        while(b_num>0&&a_num>0&&l_num>1&&o_num>1&&n_num>0){
            b_num--;
            a_num--;
            l_num = l_num-2;
            o_num = o_num-2;
            n_num--;
            ans++;
        }
        return ans;

    }

    public static void main(String[] args) {
        System.out.println(maxNumberOfBalloons("krhizmmgmcrecekgyljqkldocicziihtgpqwbticmvuyznragqoyrukzopfmjhjjxemsxmrsxuqmnkrzhgvtgdgtykhcglurvppvcwhrhrjoislonvvglhdciilduvuiebmffaagxerjeewmtcwmhmtwlxtvlbocczlrppmpjbpnifqtlninyzjtmazxdbzwxthpvrfulvrspycqcghuopjirzoeuqhetnbrcdakilzmklxwudxxhwilasbjjhhfgghogqoofsufysmcqeilaivtmfziumjloewbkjvaahsaaggteppqyuoylgpbdwqubaalfwcqrjeycjbbpifjbpigjdnnswocusuprydgrtxuaojeriigwumlovafxnpibjopjfqzrwemoinmptxddgcszmfprdrichjeqcvikynzigleaajcysusqasqadjemgnyvmzmbcfrttrzonwafrnedglhpudovigwvpimttiketopkvqw"));
    }
}
